<template>
  <div class="xtx-city" ref="target">
    <!-- 点击选择城市的按钮 -->
    <div class="select" @click="toggle" :class="{active:isShow}">
          <span v-if='!fullLocation' class="placeholder">请选择配送地址</span>
      <span v-else class="value">{{fullLocation}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <!-- 供选择的城市列表 -->
    <div class="option" v-show='isShow'>
           <!-- 判断加载的状态 -->
      <div v-if="loading" class="loading"></div>
        <span @click='changeCity(item)' v-else class="ellipsis" v-for="item in cityList" :key="item.code">{{item.name}}</span>
    </div>
  </div>
</template>
<script>
import { ref, reactive, computed } from 'vue'
import { getCityList } from '@/api/product.js'
import { onClickOutside } from '@vueuse/core'

export default {
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    const target = ref(null)
    // 监听DOM区域之外的点击操作
    onClickOutside(target, () => {
      isShow.value = false
    })

    // 获取选中的省市区所有数据
    const changeResult = reactive({
      provinceCode: '',
      provinceName: '',
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: '',
      fullLocation: ''
    })
    // 所有城市列表
    const list = ref([])
    // 调用接口的状态位
    const loading = ref(false)

    // 控制点击的切换操作
    const isShow = ref(false)
    // 控制点击弹窗的显示和隐藏
    const toggle = () => {
      isShow.value = !isShow.value
      // 弹窗显示的时候，调用接口
      if (isShow.value) {
        loading.value = true
        getCityList().then(ret => {
          list.value = ret
          loading.value = false
        })
        // 点击显示弹窗时，清空之前的数据，保证可以重新选择
        for (const key in changeResult) {
          changeResult[key] = ''
        }
      }
    }

    // 通过计算属性计算城市列表
    const cityList = computed(() => {
      // 根据所有的城市列表数据和和当前选中的值计算当前的列表信息
      let citys = list.value
      // 判断是否选中了省级单位
      if (changeResult.provinceCode && changeResult.provinceName) {
        // 选中了省，那么就计算它下面的市列表
        citys = citys.find(item => item.code === changeResult.provinceCode).areaList
      }
      // 获取县级列表数据
      if (changeResult.cityCode && changeResult.cityName) {
        // 选中了市，那么就计算它下面的县列表
        citys = citys.find(item => item.code === changeResult.cityCode).areaList
      }
      return citys
    })

    // 切换城市
    const changeCity = (item) => {
      // 判断省市区
      if (item.level === 0) {
        // 点击的是省级单位
        changeResult.provinceCode = item.code
        changeResult.provinceName = item.name
      } else if (item.level === 1) {
        // 市级单位
        changeResult.cityCode = item.code
        changeResult.cityName = item.name
      } else if (item.level === 2) {
        // 县级单位：选中省市区的结果，并且关闭弹窗
        changeResult.countyCode = item.code
        changeResult.countyName = item.name
        // 关闭弹窗
        isShow.value = false
        // 拼接完整的地址
        changeResult.fullLocation = `${changeResult.provinceName}${changeResult.cityName}${changeResult.countyName}`
        // 把最终选中的省市区所有数据传递回父组件
        emit('update-city', changeResult)
      }
    }
    return { isShow, toggle, loading, cityList, changeCity, target }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
     .loading {
      height: 290px;
      width: 100%;
      background: url(../../assets/images/loading.gif) no-repeat center;
    }
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
